package scfw.controller;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import scfw.model.SysUser;
import scfw.model.comm.JsonBean;
import scfw.service.UserServiceI;
import scfw.service.ValSetServiceI;
import scfw.service.model.LoginInfoVo;
import scfw.service.model.UserSrModel;
import scfw.utils.Constants;
import scfw.utils.LoginUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping({"/userController"})
public class UserController
{

	@Autowired
	private UserServiceI userSerive;

	@Autowired
	private ValSetServiceI valSetService;
	private static final Logger logger = LoggerFactory.getLogger(FoodController.class);

	@RequestMapping({"/{id}/showUser"})
	public String showUser(@PathVariable int id, HttpServletRequest req)
	{
		SysUser su = this.userSerive.getUserById(id);

		req.setAttribute("su", su);

		return "showUser";
	}

	@RequestMapping({"/queryUsers"})
	public String queryUsers(HttpServletRequest req)
	{
		List users = this.userSerive.querySysUser();

		req.setAttribute("users", users);

		return "queryUsers";
	}

	@RequestMapping({"/addUserOld"})
	public String addUser(Map<String, Object> map)
	{
		UserSrModel srModel = new UserSrModel();

		Map roleList = new HashMap();
		roleList.put(Integer.valueOf(1), "项目经理");
		roleList.put(Integer.valueOf(2), "部门经理");
		roleList.put(Integer.valueOf(3), "归档管理员");
		roleList.put(Integer.valueOf(4), "专家抽取员");
		roleList.put(Integer.valueOf(5), "公司总经理");
		srModel.setRoleList(roleList);

		Map ballMap = new HashMap();

		ballMap = this.valSetService.queryValSetMap("ball");
		srModel.setBallMap(ballMap);

		List svss = this.valSetService.queryValSetList("ball");
		srModel.setSvss(svss);

		Map areaList = new HashMap();

		areaList = this.valSetService.queryValSetMap("province");
		srModel.setAreaList(areaList);

		srModel.setUserName("zhangsan");
		srModel.setNickName("张三");

		srModel.setSex(Integer.valueOf(1));
		srModel.setMyBanlance(Double.valueOf(0.0D));
		srModel.setFavoriteBall(Integer.valueOf(1));
		srModel.setWorkarea("1");
		List roles = new ArrayList();
		roles.add(Integer.valueOf(1));
		srModel.setRoles(roles);

		map.put("srModel", srModel);

		return "addUser";
	}

	@RequestMapping({"/addUser"})
	public String addUserNew(Map<String, Object> map)
	{
		UserSrModel srModel = this.userSerive.addUser();

		map.put("srModel", srModel);

		return "addUser";
	}

	@RequestMapping({"/{id}/editUser"})
	public String editUser(@PathVariable int id, Map<String, Object> map)
	{
		UserSrModel srModel = this.userSerive.editUser(id);

		map.put("srModel", srModel);

		return "editUser";
	}

	@RequestMapping({"/testFtl"})
	public String testFtl(Map<String, Object> map)
	{
		map.put("username", "qiyang");

		return "testFtl";
	}

	@RequestMapping({"/submitUser"})
	@ResponseBody
	public ModelAndView submitUser(@ModelAttribute("srModel") UserSrModel srModel, JsonBean json)
	{
		try
		{
			int id = this.userSerive.submitUser(srModel);
			json.addObject("id", Integer.valueOf(id));
			json.setIsSuccess(1);
			json.setMsgInfo("操作成功！");
		} catch (Exception e) {
			json.setIsSuccess(0);
			e.printStackTrace();
		}
		System.out.println(JSON.toJSON(srModel));
		return json;
	}

	@RequestMapping({"/{id}/deleteUser"})
	@ResponseBody
	public JsonBean deleteUser(@PathVariable int id, JsonBean json)
	{
		try
		{
			this.userSerive.deleteUserById(id);
			json.setIsSuccess(1);
		} catch (Exception e) {
			json.setIsSuccess(0);
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping({"/login"})
	public String toLogin(ModelMap model) {

		String uuid= UUID.randomUUID().toString();
		model.addAttribute("random",uuid);
		model.addAttribute("checkCode", LoginUtil.getMD5(uuid+Constants.PUBLICKEY));

		return "user/login";
	}

	@RequestMapping({"/regist"})
	public String toRegist(HttpServletRequest req)
	{
		return "user/regist";
	}

	@RequestMapping({"/submitLogin"})
	@ResponseBody
	public JsonBean submitLogin(@ModelAttribute("loginForm") UserSrModel loginForm, HttpServletRequest req)
	{
		JsonBean json = new JsonBean();
		try
		{
			UserSrModel user = this.userSerive.loginUser(loginForm);
			if (user != null) {
				json.setIsSuccess(1);
				req.getSession().setAttribute("user", user);
			} else {
				json.setIsSuccess(0);
				json.setMsgInfo("用户名或密码错误！");
			}
		} catch (Exception e) {
			json.setIsSuccess(0);
			json.setMsgInfo(e.getMessage());
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping({"/submitRegist"})
	@ResponseBody
	public JsonBean submitRegist(@ModelAttribute("registForm") UserSrModel registForm, HttpServletRequest req)
	{
		JsonBean json = new JsonBean();
		try
		{
			logger.info(JSON.toJSONString(registForm));
			UserSrModel user = this.userSerive.registUser(registForm);
			json.setIsSuccess(1);
			json.setMsgInfo("恭喜您，注册成功！");
			if (user == null)
			{
				json.setIsSuccess(0);
				json.setMsgInfo("系统异常，请联系管理员！");
			}
		} catch (Exception e) {
			json.setIsSuccess(0);
			json.setMsgInfo(e.getMessage());
			e.printStackTrace();
		}
		return json;
	}



	@RequestMapping({"/loginOut"})
	@ResponseBody
	public JsonBean loginOut(HttpServletRequest req)
	{
		JsonBean json = new JsonBean();
		try {
			req.getSession().removeAttribute("user");
		} catch (Exception e) {
			json.setIsSuccess(0);
			json.setMsgInfo(e.getMessage());
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping({"/ssoLogin"})
	@ResponseBody
	public JsonBean ssoLogin(HttpServletRequest req, LoginInfoVo loginInfoVo) {
		logger.info(">>>>>SSO单点登录");
		JsonBean json = new JsonBean();
		try {

			//验证模拟登录信息
			String random = loginInfoVo.getRandom();
			String publicKeyCheckCode = LoginUtil.getMD5(loginInfoVo.getUsername() + random + Constants.PUBLICKEY);
			String checkCode = loginInfoVo.getCheckCode();
			if (!publicKeyCheckCode.equals(checkCode)) {
				throw new Exception("非法请求");
			}
			//进行模拟登录
			UserSrModel user = this.userSerive.ssoLogin(loginInfoVo);
			if (user != null) {
				json.setIsSuccess(1);
				req.getSession().setAttribute("user", user);
			}

		} catch (Exception e) {
			json.setIsSuccess(0);
			json.setMsgInfo(e.getMessage());
			e.printStackTrace();
		}
		return json;
	}
}